package cnpc.udp.example.service.impl;

import cnpc.udp.example.convert.TestConvert;
import cnpc.udp.example.mapper.TestMapper;
import cnpc.udp.example.controller.vo.TestPageVO;
import cnpc.udp.example.controller.vo.TestVO;
import cnpc.udp.example.dataobject.TestDO;
import cnpc.udp.example.service.TestService;
import cnpc.udp.framework.basic.common.datastructure.PageResult;
import cnpc.udp.framework.mybatis.core.service.BaseServicexImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName TestServiceImpl
 * @Description TODO
 * @Author Tiansai
 * @Date 2024年05月24日 11:03
 **/
@Service
public class TestServiceImpl extends BaseServicexImpl<TestMapper, TestDO> implements TestService {
    @Resource
    private TestMapper testMapper;

    @Override
    public void insert(TestVO vo) {
        TestDO testDO = TestConvert.INSTANCE.convertDo(vo);
        testMapper.insert(testDO);
    }

    @Override
    public List<TestVO> getList() {

        return TestConvert.INSTANCE.convertVos(testMapper.selectList1());
    }

    @Override
    public PageResult<TestDO> getPage(TestPageVO reqVO) {
        return testMapper.selectPage(reqVO);
    }

    @Override
    public PageResult<TestDO> getPage2(TestPageVO reqVO) {
        // mybatis 常规分页方式，需要具备mybatis基础知识
        int count = testMapper.count();
        int pageSize = reqVO.getPageSize();  // 每页显示的记录数
        int pageNo = reqVO.getPageNo();     // 要获取的页码

        int offset = (pageNo - 1) * pageSize;  // 计算偏移量
        int limit = pageSize;                  // 分页大小
        List<TestDO> list = testMapper.selectByPage(offset, limit);

        return new PageResult<>(count,list);
    }

}
